<?php
namespace app\madmin\controller;
use app\madmin\model\SystemMemberModel;
use app\madmin\model\SystemGroupModel;
use think\Controller;
use org\Verify;

/**
 * 登录控制器
 * @author  黄明杰 vx:HMJ125855
 * @version 20190101
 */
class SystemLogin extends Controller{

    # 访问空的方法
    public function _empty(){
        return $this->fetch('PublicBase/show404');
    }

    # 页面跳转方法
    public function index(){
        $this->redirect('SystemLogin/showOn');
    }

    # 进入登陆方法
    public function showOn(){
        if(request()->isPost()){
            $Code = 2;
            $Msgs = '';
            $Urls = '';
            $Time = time();

            $User = input('post.admin_user','','trim');
            $Pass = input('post.admin_pass','','trim');
            $Nums = input('post.admin_code','','trim');

            if(empty($User)){
                $this->ShowData($Code, lang('XtDlZhWk'));
            }
            if(empty($Pass)){
                $this->ShowData($Code, lang('XtDlMmWk'));
            }
            if(empty($Nums)){
                $this->ShowData($Code, lang('XtDlYzmWk'));
            }
            if(!$this->showCheck($Nums)){
                $this->ShowData($Code, lang('XtDlYzmCw'));
            }

            $SystemMember = new SystemMemberModel;
            $FindMember = $SystemMember->mInfo(['member_user'=>$User]);

            if(empty($FindMember)){
                $Msgs = lang('XtDlZhCw');
            }else{
                if($FindMember['member_error'] >= 5){
                    $Msgs = lang('XtDlZhDj');
                }else{
                    $SystemGroup = new SystemGroupModel;
                    $FindGroup = $SystemGroup->mInfo(['group_id'=>$FindMember['member_group']]);
                    if($FindMember['member_group'] == 0 || $FindGroup['group_state'] == 2){
                        $Msgs = lang('XtDlZhWq');
                    }else{
                        if($FindMember['member_state'] == 1){
                            if($FindMember['member_pass'] != md5MemberPass($Pass)){
                                $Msgs = lang('XtDlMmCw');
                            }else{
                                $Code = 1;
                                $Msgs = lang('XtDlHtCg');
                                $Urls = url('Index/index');

                                $Param = [];
                                $Param['member_login'] = $Time;
                                $Param['member_time'] = $FindMember['member_time'] + 1;
                                $Param['member_error'] = 0;
                                $Param['member_ip'] = request()->ip();

                                $SystemMember->mUpdate(['member_id'=>$FindMember['member_id']], $Param);

                                # 生成令牌
                                $Token = md5MemberToken($Time, rand(100000, 999999));

                                session('AdminID', $FindMember['member_id']);
                                session('AdminUser', $FindMember['member_user']);
                                session('AdminRule', $FindGroup['group_rules']);
                                session('AdminToken', $Token);

                                # 缓存令牌
                                $Array = [];
                                $Array[$FindMember['member_id']] = $Token;
                                cache('SystemToken', $Array);
                            }
                        }else{
                            $Msgs = lang('XtDlZhJy');
                        }
                    }

                    if($Code == 2){
                        $SystemMember->mAddSub(1, ['member_id'=>$FindMember['member_id']], 'member_error');
                    }
                }

                addMemberLog($FindMember['member_id'], 1, $Msgs, $Code);
            }

            $this->ShowData($Code, $Msgs, $Urls);
        }else{
            return $this->fetch('SystemLogin/showOn');
        }
    }

    # 退出登陆方法
    public function showOff(){
        if(request()->isPost()){
            $UID = session('AdminID');//唯一标识

            session(null);//清空Session

            cache('Menus_'.$UID, Null);//清空成员菜单缓存
            
            $this->ShowData(1, lang('XtDlDcCg'), url('SystemLogin/showOn'));
        }
    }

    # 用户验证方法
    public function showVerify(){
        $Verify = new Verify(config('verification_code'));

        $Verify->entry(1);
    }

    /**
     * 用户验证方法
     * @param   Int     $Code       验证内容
     * @param   Int     $ID         唯一标识
     * @return  Bool
     */
    public function showCheck($Code, $ID = 1){
        $Verify = new Verify;

        return $Verify->check($Code, $ID);
    }

}